import { createRouter, createWebHashHistory, RouteRecordRaw } from 'vue-router'
import localCache from '@/utils/cache'
const routes: Array<RouteRecordRaw> = [
  {
    path: '/',
    redirect: '/Home',
  },
  {
    path: '/login',
    name: 'login',
    component: () => import('@/views/login/login.vue'),
  },
  {
    path: '/setting',
    name: 'setting',
    component: () => import('@/views/setting/setting.vue'),
    children: [
      {
        path: 'tenant',
        name: '租户',
        component: () => import('@/views/setting/tenant/tenant.vue'),
      },
      {
        path: 'role',
        name: '角色',
        component: () => import('@/views/setting/role/role.vue'),
      },
      {
        path: 'user',
        name: '用户',
        component: () => import('@/views/setting/user/user.vue'),
      },
    ],
  },
  {
    path: '/Home',
    name: 'Home',
    component: () => import('@/views/Home/Home.vue'),
    children: [
      {
        path: '',
        redirect: '/Home/main',
      },
      {
        path: 'main',
        name: 'main',
        redirect: '/Home/main/content',
        component: () => import('@/views/Home/main/main.vue'),
        children: [
          {
            path: 'policy',
            name: 'policy',
            component: () => import('@/views/Home/policy/policy.vue'),
            children: [
              {
                path: 'submodule',
                name: 'submodule',
                component: () =>
                  import('@/views/Home/policy/submodule/submodule.vue'),
              },
            ],
          },
          {
            path: 'content',
            name: 'content',
            component: () => import('@/views/Home/main/content/content.vue'),
          },
        ],
      },

      {
        path: 'Custome',
        name: 'Custome',
        component: () => import('@/views/Home/Custome/Custome.vue'),
      },
    ],
  },
  {
    path: '/:pathMatch(.*)*',
    name: 'not-found',
    component: () => import('@/views/not-found/not-found.vue'),
  },
]

const router = createRouter({
  // history: createWebHistory(process.env.BASE_URL),
  history: createWebHashHistory(),
  routes,
})
router.beforeEach((to) => {
  // 判断是否登录 没有登录跳转到登录页
  if (to.path == '/setting') {
    const token = localCache.getCache('token')
    if (!token) {
      return '/Home/main'
    }
  }

  // 判断进入第一个路由
  // if (to.path === '/main') {
  //   return firstMenu.url
  // }
})
export default router
